package com.base.system.managedBean;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.DepartmentalUnit;
import com.base.organization.service.IDepartmentalUnitDAO;
import com.base.security.SecurityConst;
import com.base.system.managedBean.baseBean.BaseSecurityDepartmentBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_addDepartmentalUnitBean", scope = ManagedBeanScope.REQUEST)
public class AddDepartmentalUnitBean extends BaseSecurityDepartmentBean {
	Log logger = LogFactory.getLog(AddDepartmentalUnitBean.class);
	private IDepartmentalUnitDAO departmentalUnitDAO = SpringBeanHolder
			.getBean("departmentalUnitDAO");
	@Inject
	private FacesContext context;

	@Bind
	private String parentDepartmentalUnitId_name;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			parentDepartmentalUnitId = context.getExternalContext()
					.getRequestParameterMap().get("parentDepartmentalUnitId");
			if (parentDepartmentalUnitId != null) {
				DepartmentalUnit du = departmentalUnitDAO.getById(Integer
						.parseInt(parentDepartmentalUnitId));
				if (du != null) {
					parentDepartmentalUnitId_name = du.getName();
				}
			}
		}
	}

	protected void initSpecialProperty() {
	}

	@Action(id = "saveDepartmentalUnit")
	public void saveDepartmentalUnit() {
		departmentalUnit = new DepartmentalUnit();
		this.setSecurityDepartmentProperty();
		departmentalUnitDAO.insert(departmentalUnit);
		context.getExternalContext().getSessionMap().put(
				SecurityConst.DEPARTMENTALUNIT_SELECT_ROW_DATA,
				departmentalUnit);
		String script = "closeCenterLayoutTab('"
				+ SecurityConst.DEPARTMENTALUNIT_ADD_TAB + "')";
		showDepartmentalUnit();
		Browser.execClientScript("window.top", script);
	}

	public void showDepartmentalUnit() {
		String url = "/modules/system/showDepartmentalUnit.faces";
		url = context.getExternalContext().getRequestContextPath() + url;
		String script = "addCenterLayoutTab('"
				+ SecurityConst.DEPARTMENTALUNIT_SHOW_TAB + "','" + "查看部门"
				+ "', '" + url + "')";
		Browser.execClientScript("window.top", script);
	}
}
